define(
		[ "PDUtilDir/dataService", "PDUtilDir/grid", "PDUtilDir/slidebar" ],
		function(DataService, Grid, Slidebar) {
			function ChoiceVisitorSupportInit($scope, $compile) {
				return new ChoiceVisitorSupport($scope, $compile);
			}
			function ChoiceVisitorSupport($scope, $compile) {
				this.$scope = $scope;
				this.$compile = $compile;
			}
			ChoiceVisitorSupport.fn = ChoiceVisitorSupport.prototype;
			ChoiceVisitorSupport.fn.init = function() {
				this.entity = {
					visitors : undefined,
				};
				this.saerch = {};
			}
			ChoiceVisitorSupport.fn.digest = function() {
				this.$scope.$digest();
			}
			ChoiceVisitorSupport.fn.run = function() {
				var _this = this;
				_this.init();
				_this.slidebars = Slidebar({
					size : 'lg',
					url : getServer() + "/static/app/product/reserveInfos/views/addVisitor.html",
//					close : false,
					$scope : this.$scope,
					$compile : this.$compile,
					afterLoad : function() {
						_this.initialization();
						_this.digest();
					}
				});
			};
			ChoiceVisitorSupport.fn.initialization = function() {
				var _this = this;
				_this.grid = Grid({
					id : "VisitorForSearchGrid",
					multi : true,
					pagination : true,
                    index : true,
					cache : false,
					onSeletedChanged : function(data, data1) {
						var len = data.length;
						if (len == 0) {
							_this.entity.visitors = new Array();
							_this.$scope.addVisitorForm.visitorIds.$setViewValue(undefined);
							return;
						}
						_this.$scope.addVisitorForm.visitorIds.$setViewValue(true);
						_this.entity.visitors = data;
					},
					layout : [ {
						name : "中文名",
						field : "zhName",
						width: "60px",
						sort : true
					}, {
						name : "英文名",
						field : "enName",
						width: "60px",
						sort : true
					}, {
						name : "性别",
						field : "sex",
						width: "40px",
						format : function(e) {
							if (e.row.sex == "1" || e.row.sex == 1)
								return '男';
							else
								return '女';
						}
					}, {
						name : "年龄",
						width: "40px",
						field : "age"
					}, {
						name : "联系电话",
						width: "80px",
						field : "phone"
					}, {
						name : "证件类型",
						field : "certificates",
						width: "60px",
						format:function(e){
							return _this.$scope.cache.certificatesObject.get(e.row.certificates);
						}
					}, {
						name : "证件号码",
						field : "certificatesNumber"
					} ],
					toolbar : [ {
						name : "查询",
						icon : "fa fa-search",
						callback : function(event) {
							_this.searchFun();
						}
					} ],
					data : {
						type : 'URL',
						value : getServer() + '/flea/hclv/visitor/selectByDept.htmls'
					},
                    onLoadDataSuccess:function(data){
                        var selecteds = _this.$scope.cache.visitor,
                            len = selecteds.length,
							i = 0;

                        if(len == 0)
                        	return data;

                        for(var n = 0; n < data.length; n++){
                            for(i = 0; i < len; i++){
                                if (data[n] && data[n].id == selecteds[i].id){
                                    data.splice(n, 1);
                                }
                            }
                        }
						return data;
					}
				});
//				_this.searchFun();
			};
			ChoiceVisitorSupport.fn.searchFun = function() {
				this.grid.reload({
					queryParam : this.search
				})
			};
			ChoiceVisitorSupport.fn.save = function() {
				DataService.setObject('product.search.reserve.visitors', this.entity.visitors);
				this.$scope.reloadSupport.run();
				this.slidebars.close();
			};
			return ChoiceVisitorSupportInit;
		});